package com.rs.controller;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.rs.model.CommonResult;
import com.rs.model.ZhUser;
import com.rs.service.PublicUserMethodsService;
import com.rs.service.ZhUserStudentService;
import com.rs.util.DateFormatUtil;
import com.rs.util.SpringContextUtils;

import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;

public class UserAuditInfo extends JFrame {
	
	SelectUserAudit ss;
	
	private ZhUserStudentService zhUserService = SpringContextUtils.getBean(ZhUserStudentService.class);
	private PublicUserMethodsService pum = SpringContextUtils.getBean(PublicUserMethodsService.class);
	
	private JPanel contentPane;
	
	private JTextField text_username;
	private JTextField text_pass;
	private JTextField text_name;
	private JTextField text_phone;
	private JTextField text_card;
	private JTextField text_sex;
	private JTextField text_age;
	private JTextField text_img;
	private JTextField text_de;
	private JTextField text_address;
	private JTextField text_po;
	private JTextField text_class;
	private JTextField text_day;
	private JTextField text_studyType;
	private JLabel lblNewLabel_img ;
	private ImageIcon ico;
	
	private int id;//用户id
	private ZhUser user;//审核人
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserAuditInfo frame = new UserAuditInfo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UserAuditInfo() {
		aa();
	}
	
	
	public UserAuditInfo(Integer id,SelectUserAudit ss,ZhUser user) {
		this.user=user;
		this.id = id;
		this.ss=ss;
		aa();
	}
	private void aa(){
		setBounds(100, 100, 657, 600);
		getContentPane().setLayout(null);
		
		
		JLabel label = new JLabel("\u8D26\u53F7\uFF1A");
		label.setBounds(45, 82, 54, 15);
		getContentPane().add(label);
		
		JLabel lblNewLabel = new JLabel("\u5BC6\u7801\uFF1A");
		lblNewLabel.setBounds(45, 136, 54, 15);
		getContentPane().add(lblNewLabel);
		
		JLabel label_1 = new JLabel("\u59D3\u540D\uFF1A");
		label_1.setBounds(45, 188, 54, 15);
		getContentPane().add(label_1);
		
		JLabel label_2 = new JLabel("\u624B\u673A\u53F7\uFF1A");
		label_2.setBounds(45, 247, 54, 15);
		getContentPane().add(label_2);
		
		JLabel label_3 = new JLabel("\u8EAB\u4EFD\u8BC1\u53F7\uFF1A");
		label_3.setBounds(45, 301, 67, 15);
		getContentPane().add(label_3);
		
		JLabel label_4 = new JLabel("\u6027\u522B\uFF1A");
		label_4.setBounds(45, 354, 54, 15);
		getContentPane().add(label_4);
		
		JLabel label_5 = new JLabel("\u5E74\u9F84;");
		label_5.setBounds(45, 406, 54, 15);
		getContentPane().add(label_5);
		
		text_username = new JTextField();
		text_username.setBounds(114, 79, 148, 27);
		getContentPane().add(text_username);
		text_username.setColumns(10);
		
		text_pass = new JTextField();
		text_pass.setColumns(10);
		text_pass.setBounds(114, 130, 148, 27);
		
		getContentPane().add(text_pass);
		
		text_name = new JTextField();
		text_name.setColumns(10);
		text_name.setBounds(114, 182, 148, 27);
		getContentPane().add(text_name);
		
		text_phone = new JTextField();
		text_phone.setColumns(10);
		text_phone.setBounds(114, 244, 148, 27);
		getContentPane().add(text_phone);
		
		text_card = new JTextField();
		text_card.setColumns(10);
		text_card.setBounds(114, 295, 148, 27);
		getContentPane().add(text_card);
		
		text_sex = new JTextField();
		text_sex.setColumns(10);
		text_sex.setBounds(114, 348, 148, 27);
		getContentPane().add(text_sex);
		
		text_age = new JTextField();
		text_age.setColumns(10);
		text_age.setBounds(114, 400, 148, 27);
		getContentPane().add(text_age);
		
		JLabel label_6 = new JLabel("\u4F4F\u5740\uFF1A");
		label_6.setBounds(338, 82, 54, 15);
		getContentPane().add(label_6);
		
		JLabel label_7 = new JLabel("\u5934\u50CF\uFF1A");
		label_7.setBounds(338, 354, 54, 15);
		getContentPane().add(label_7);
		
		JLabel label_8 = new JLabel("\u90E8\u95E8\uFF1A");
		label_8.setBounds(338, 136, 54, 15);
		getContentPane().add(label_8);
		
		JLabel label_9 = new JLabel("\u804C\u52A1\uFF1A");
		label_9.setBounds(338, 188, 66, 15);
		getContentPane().add(label_9);
		
		JLabel label_10 = new JLabel("\u51FA\u751F\u65E5\u671F\uFF1A");
		label_10.setBounds(322, 301, 66, 15);
		getContentPane().add(label_10);
		
		JLabel label_11 = new JLabel("班级：");
		label_11.setBounds(338, 247, 66, 15);
		getContentPane().add(label_11);
		
		JLabel label_12 = new JLabel("人员审核");
		label_12.setFont(new Font("宋体", Font.PLAIN, 16));
		label_12.setHorizontalAlignment(SwingConstants.CENTER);
		label_12.setBounds(257, 24, 103, 33);
		getContentPane().add(label_12);
		
		text_address = new JTextField();
		text_address.setColumns(10);
		text_address.setBounds(391, 79, 148, 27);
		getContentPane().add(text_address);
		
		text_de = new JTextField();
		text_de.setColumns(10);
		text_de.setBounds(391, 130, 148, 27);
		getContentPane().add(text_de);
		
		text_po = new JTextField();
		text_po.setColumns(10);
		text_po.setBounds(391, 182, 148, 27);
		getContentPane().add(text_po);
		
		text_class = new JTextField();
		text_class.setColumns(10);
		text_class.setBounds(391, 241, 148, 27);
		getContentPane().add(text_class);
		
		text_day = new JTextField();
		text_day.setColumns(10);
		text_day.setBounds(391, 295, 152, 27);
		getContentPane().add(text_day);
		
		JButton button_pass = new JButton("通过");
		button_pass.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userAudit(1);
			}
		});
		button_pass.setBounds(459, 492, 78, 33);
		getContentPane().add(button_pass);
		
		JButton button_turn = new JButton("驳回");
		button_turn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userAudit(2);
			}
		});
		button_turn.setBounds(371, 492, 78, 33);
		getContentPane().add(button_turn);
		
		ico=new ImageIcon("src/main/java/images/null.jpg");
		ico.setImage(ico.getImage().getScaledInstance(135,95,Image.SCALE_DEFAULT));
		
		lblNewLabel_img = new JLabel(ico);
		lblNewLabel_img.setBounds(391, 354, 135, 95);
		getContentPane().add(lblNewLabel_img);
		
		setTheValue(getIdUser());
	}
	
	//根据id得到用户详细
	private ZhUser getIdUser(){
		ZhUser user =new ZhUser();
		user.setuId(id);
		CommonResult<ZhUser>  com = zhUserService.selectStudent(user);
		return com.getData();
	}
	
	//渲染数据
	private void setTheValue(ZhUser zh){
		if(zh.getuStudyType() !=null){	
			text_studyType = new JTextField();
			text_studyType.setColumns(10);
			text_studyType.setBounds(114, 498, 148, 27);
			getContentPane().add(text_studyType);
			
			JLabel label_14 = new JLabel("学习专业：");
			label_14.setBounds(45, 501, 67, 15);
			getContentPane().add(label_14);
			text_studyType.setEditable(false);
		}
		text_username.setText(zh.getuUsername());
		text_pass.setText(text_pass.getText());
		text_name.setText(zh.getuName());
		text_phone.setText(zh.getuPhone());
		text_card.setText(zh.getuIdcard());
		text_sex.setText(zh.getuSex());
		text_age.setText(zh.getuAge()+"");
		if(zh.getuHead() !=null && zh.getuHead().length() !=0){
			ico.setImage(Toolkit.getDefaultToolkit().getImage(zh.getuHead()));
			ico.setImage(ico.getImage().getScaledInstance(135,95,Image.SCALE_DEFAULT));
		}
		
		text_address.setText(zh.getuAddress());
		text_de.setText(pum.selectDe(zh.getuDeid()));
		text_po.setText(pum.selectPo(zh.getuPoid()));
		if(zh.getuStudyType() !=null &&zh.getuStudyType()==1){
			text_studyType.setText("java");
		}else if(zh.getuStudyType() !=null && zh.getuStudyType()==2){
			text_studyType.setText("php");
		}
		
		if(zh.getuCaid() !=null){
		text_class.setText(pum.selectCl(zh.getuCaid()));
		}
		if(zh.getuBirthday() !=null){
			text_day.setText(DateFormatUtil.getDateStringw(zh.getuBirthday(),"yyyy-MM-dd"));
		}
		text_username.setEditable(false);
		text_pass.setEditable(false);
		text_name.setEditable(false);
		text_phone.setEditable(false);
		text_card.setEditable(false);
		text_sex.setEditable(false);
		text_age.setEditable(false);
		
		text_de.setEditable(false);
		text_address.setEditable(false);
		text_po.setEditable(false);
		text_class.setEditable(false);
		text_day.setEditable(false);
		
	}
	
	//审核  通过或驳回
	private void userAudit(Integer uexamin){
		ZhUser user = new ZhUser();
		user.setuId(id);
		user.setuUid(this.user.getuId());
		user.setuExamine(uexamin);
		CommonResult<Integer> com  = zhUserService.updateStudent(user);	
		if(com.getData()>0){
			alert("操作成功！");
			ss.dynamicRefresh(id);
		}
	}

	private void alert(String aa) {
		JOptionPane.showMessageDialog(this, aa);
	}
}
